package org.xtremeobjects.domain.person;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.xtremeobjects.anotations.ddd.Repository;
import org.xtremeobjects.domain.hibernate.HibernateSession;
import org.xtremeobjects.domain.party.PartyRepositoryHibernate;

import com.google.inject.Inject;

@Repository(root=Person.class)
public class PersonRepositoryHibernate extends PartyRepositoryHibernate implements PersonRepository {

	@Inject
	public PersonRepositoryHibernate(HibernateSession session) {
		super(session);
	}

	public List<Person> findPeopleByAge(int from, int to) {
		throw new RuntimeException("Not implemented");
	}

	public List<Person> findPeopleByCity(String city) {
		return getSession().createCriteria(Person.class).createCriteria("contactDetails").createCriteria("addresses").add(Restrictions.eq("city", city)).list();
	}

	public List<Person> findPeopleByEmail(String email) {
		return getSession().createCriteria(Person.class).createCriteria("contactDetails").createCriteria("emails").add(Restrictions.eq("email", email)).list();
	}

	public List<Person> findPeopleByNameLike(String name) {
		return getSession().createCriteria(Person.class).add(Restrictions.like("name", "%" + name + "%")).list();
	}

	public List<Person> findPeopleByPhone(String phone) {
		return getSession().createCriteria(Person.class).createCriteria("contactDetails").createCriteria("phones").add(Restrictions.eq("number", phone)).list();
	}

	public List<Person> findPeopleByZipCode(String zipcode) {
		return getSession().createCriteria(Person.class).createCriteria("contactDetails").createCriteria("addresses").add(Restrictions.eq("zipcode", zipcode)).list();
	}

	public Person findPersonById(String id) {
		return (Person)getSession().createCriteria(Person.class).add(Restrictions.eq("id", id)).uniqueResult();
	}

}
